package SingleLinkedList;


public class MySingleLinkedList {
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val){
            this.val = val;
        }
    }

    public ListNode head;

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;

    }

    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
            return;
        }
        ListNode cur = head;
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }

    private boolean checkPosInIndex(int index){
        int len = size();
        if(index < 0 || index > len){
            throw new PosIllegal("index位置不合法！");
        }
        return true;
    }
    // 任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        try {
            checkPosInIndex(index);
            if(index == 0){
                addFirst(data);
                return;
            }
            ListNode cur = head;
            ListNode node = new ListNode(data);
            for (int i = 0; i < index - 1; i++) {
                cur = cur.next;
            }
            node.next = cur.next;
            cur.next = node;
        }catch (PosIllegal e){
            System.out.println("addIndex中index位置不合法！");
            e.printStackTrace();
        }
    }

    
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;

    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head == null){
            return;
        }
        if (head.val == key){
            head = head.next;
            return;
        }
        ListNode cur = head;
        while(cur.next != null){
            if(cur.next.val == key){
                ListNode del = cur.next;
                cur.next = del.next;
            }
            cur = cur.next;
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null){
            return;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while(cur != null){
            if(cur.val == key){
                cur = cur.next;
                prev.next = cur;
            } else {
                cur = cur.next;
                prev = prev.next;
            }
        }
        if(head.val == key){
           head = head.next;
        }
    }

    //得到单链表的长度
    public int size(){
        int len = 0;
        ListNode cur = head;
        while(cur != null){
            len++;
            cur = cur.next;
        }
        return len;
    }

    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public  void display2(ListNode newHead){
        ListNode cur = newHead;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void clear(){
        ListNode cur = head;
        while (cur != null){
            ListNode curNext = cur.next;
            cur.next = null;
            cur = curNext;
        }
        head = null;
    }

    public ListNode partition(int x) {
        // write code here
        ListNode beforeStart = null ;
        ListNode beforeEnd = null;
        ListNode afterStart = null;
        ListNode afterEnd = null;
        ListNode cur = head;
        while(cur != null){
            if(cur.val >= x) {
                if(afterStart == null){
                    afterStart = cur;
                    afterEnd = cur;
                } else{
                    afterEnd.next = cur;
                    afterEnd = afterEnd.next;
                }
            }
            else {
                if(beforeStart == null){
                    beforeEnd = beforeStart = cur;
                } else{
                    beforeEnd.next = cur;
                    beforeEnd = beforeEnd.next;
                }
            }
            cur = cur.next;
        }
        if(beforeStart == null){
            return afterStart;
        }
        beforeEnd.next = afterStart;
        if(afterStart != null){
            afterEnd.next = null;
        }
        return beforeStart;
    }
}
